(function(){
	  var page = 1;
	  var lock = true;
	  var classifyId = window.location.hash.slice(1);
	  var recommendationType = "";
	  var maxpage;
	  var minpage;
	  function getSource(fn){
		  
	   $.get('../../portal/resource/fillProtalList.json?pageSize=12&classifyId=' + classifyId + "&orderType=" + recommendationType + "&currentPage="+ page ,function(datas){
		  
		 if(datas.success){
  		  renderAside(datas);
		  maxpage = datas.data.resourceQuery.maxPage;
		  minpage = datas.data.resourceQuery.minPage;
		  if(fn){fn(maxpage)};
		   for(var i = 0 ; i < datas.data.resourceList.length; i++){
			   if(datas.data.resourceList[i].resourceTitle.length<8){
			    	datas.data.resourceList[i].title = datas.data.resourceList[i].resourceTitle;
			    }else{
			    	datas.data.resourceList[i].title = datas.data.resourceList[i].resourceTitle.substring(0,8)+"...";
			    }
			   if(datas.data.resourceList[i].fileSize == 0){
				   datas.data.resourceList[i].fileSize = 1 ;
			   }
			   datas.data.resourceList[i].fileSize = dealByte(datas.data.resourceList[i].fileSize);
			    var arr = ['null','vido','music'];
			    //判断点击类型是否是3
			    if(datas.data.resourceList[i].resourceType === 3){
			    	datas.data.resourceList[i].resourceFormat = datas.data.resourceList[i].resourceFormat || 'doc';
			    	var type = datas.data.resourceList[i].resourceFormat.toLowerCase();
			    	var id = datas.data.resourceList[i].fileId || '';
			    	datas.data.resourceList[i].href = 'components/gtattachment/open/downloadAttachment.json?attachmentId=' + id;
			    	 switch(type){
			    	   case 'doc':
			    	   case 'docx':
			    	   datas.data.resourceList[i].typeClass = 'word';
			    	   break;
			    	   case 'ppt':
			    	   case 'pptx':
			    	   case 'pps':
			    	   case  'ppsx':
			    	   datas.data.resourceList[i].typeClass = 'ppt';
			    	   break;
			    	   case 'txt':
			    	   datas.data.resourceList[i].typeClass = 'txt';
			    	   break;
			    	   case 'pdf':
			    	   datas.data.resourceList[i].typeClass = 'pdf';
			    	   break;
			    	   case  'pxt':
			    	   datas.data.resourceList[i].typeClass = 'pxt'; 
			    	   break;
			    	   case 'xls':
			    	   case 'xlsx':
			    	   datas.data.resourceList[i].typeClass = 'xian';
			    	   break;
			    	 }
			    	
			    	 continue;
			    }
			    //防止资源未上传，路径是undefined；
			    var idx = datas.data.resourceList[i].resourceId || '';
		    	datas.data.resourceList[i].href = 'portal/resource/downloadCurrentResource?resourceId=' + idx;
			    datas.data.resourceList[i].typeClass = arr[datas.data.resourceList[i].resourceType] || 'vido';
		   }
		   page = datas.data.resourceQuery.currentPage;
		
		   //防止二次加载
		  if(lock){
			  var typeResourceDom = _.template($('#type-resource').html())(datas.data)
			   $('.top_box').html(typeResourceDom );  
		  } 	
		    
		   var downLoadResourceDom = _.template($('#downLoad-resource').html())(datas.data);
		  		   
		   $('.downLoadList').html(downLoadResourceDom);
		
		 //为添加的新dom追加事件
		   $(document).ready(function(){
			   if(!lock) return;
			   lock = false;
			   $(".other").click(function(){
			      if($(".weikeTag").css("max-height") == 'none'){
			           $(".weikeTag").css({"max-height":"30px"});
			           $(this).find(".glyphicon").removeClass("glyphicon-menu-down").addClass('glyphicon-menu-up');
			      }else{
			           $(".weikeTag").css({"max-height":"none"});
			           $(this).find(".glyphicon").removeClass("glyphicon-menu-up").addClass('glyphicon-menu-down');
			      }
			   });
			   //分类点击事件
			   $('.weikeTag li a').click(function(){
				   
				  $('.all').addClass('allSource');
		        
				 $(this).attr('class','ect');
				 
				 $(this).parent('li').siblings().children().attr('class','default');
				   classifyId = $(this).attr('typeid') || "";
				   page = 1;
				   getSource(pageComponents);
				   
			   })
			   //推荐点击事件
			   $('.recommendation li a').click(function(){
				   
				   $('.allLoad').addClass('allSourceLoad');
				   
				   $(this).attr('class','ect');	
				   
				   $(this).parent('li').siblings().children().attr('class','default');
				   
				   recommendationType = $(this).attr('typeid') || "";
				   
				   page = 1;
				   
				   getSource(pageComponents);
				    
				    
			   })

			   //为全部按钮绑定事件；
			   $(".all").click(function(){
				   $(this).removeClass('allSource');
				   $('.weikeTag li a').removeClass('ect').addClass('default');
				   classifyId = "";
				   page = 1;
				   getSource(pageComponents);
			   })
			   $('.allLoad').click(function(){
				    $(this).removeClass('allSourceLoad');
				   $('.recommendation li a').removeClass('ect').addClass('default');
				   recommendationType = "";
				   page = 1;
				   getSource(pageComponents);
			   })
			 });
		   $(".courseAsideList").each(function(e){
			   $(this).find(".list-show").css("margin-top",38*e+38+"px");
		   });
		 }
	   })
	
	  }	
	  getSource(pageComponents);
	  
	  
	  
	  
	  function pageComponents(maxPage){
		  var maxPage = maxPage;
		  //将翻页组件插入进来
		  var components = new PageNum(maxPage);
		  //为翻页添加事件；
		  components.events(function(num){
			      page = num;
				  getSource();
		  })
	  }
	  
	  function dealByte(parameter){    
	    var fileKb=parseInt(parameter/1024);
	    var fileMb=parseInt(parameter/(1024*1024));
	    if(fileKb>0&&fileMb>0){
	      fileKb=fileKb-(1024*fileMb);
	      if(fileKb == 0){
	    	  return fileMb+"MB";
	      }
	      return fileMb+"MB "+fileKb+"KB";
	    }
	    else if(fileKb>0&&fileMb==0){
	      return fileKb+"KB";
	    }
	    else if(parameter>0&&fileKb==0){
	      return parameter+"BYTE";
	    }
	    else{
	      return;
	    }    
	  }
	  
	  /**************分类********************/
	  //最新最热的标签事件
	   $('#newEst').click(function(){
		   if(recommendationType == 'laster'){return};
		   recommendationType = 'laster';
		   page = 1;
		   getSource(pageComponents);
		   $(this).addClass('active').siblings().removeClass('active');
		   $('#curricula').removeClass('active');
		  // console.log($('#curricula'))
	   })
	   $("#hotTest").click(function(){
		   if(recommendationType == 'hot'){return};
		   recommendationType = 'hot';
		   page = 1;
		   getSource(pageComponents);
		   $(this).addClass('active').siblings().removeClass('active');
		   $('#curricula').removeClass('active');
	   })
	  
	  crumbsNav();
	   //全部课程标签
	    $('.resourceContent').delegate('.allCourse','click',function(){
	    	if($(this).hasClass('Taxonomy'))return;
	    	page = 1;
	    	hotType = '';
	    	classifyId = '';
	    	getSource(pageComponents);
	    	$('.center-title li').removeClass('active');
	    	$(this).parent('#curricula').addClass('active');
	    	$('#curricula .Taxonomy').text('全部课程').removeClass('Taxonomy').addClass('allCourse');
	    	crumbsNav();
	    })
	    
	    //分类课程标签
	    $('#curricula').delegate('.Taxonomy','click',function(){
	    	   if($(this).hasClass('allCourse'))return;
	    	   classifyId = $(this).attr('classifyId');
	    	   $(this).parent('#curricula').addClass('active');
			   page = 1;
			   hotType = '';
			   getSource(pageComponents);
			   $('.center-title li').removeClass('active');
	    })

	   //侧边栏点击事件
		   $('.bd').delegate('.Taxonomy','click',function(){
			   classifyId = $(this).attr('classifyId');
			   page = 1;
			   hotType = '';
			   $('#curricula').addClass('active');
			   $('.center-title li').removeClass('active');
			   getSource(pageComponents);
			   crumbsNav(classifyId);
		   })
	   //课程库面包屑导航
	   function crumbsNav(id){
		   if(!id){
			var crumbsNavDOM = _.template($('#crumbsNav').html())({data:[]});
			$('#breadChippingBox').html(crumbsNavDOM);
			return;
		   }
		   var courseClassify = $('#allCourse');
		   $.ajax({
			   url:'../../portal/resource/findClassifyNav.json',
			   type:'GET',
			   data:{
				  classifyId : id
			   },
			   dataType:'json',
			   success:function(result){
				 if(result.success){
					var crumbsNavDOM = _.template($('#crumbsNav').html())(result);
					$('#breadChippingBox').html(crumbsNavDOM)
					if(result.data.length >= 1){
						var classIfyObj = result.data[result.data.length-1];
						courseClassify.text(classIfyObj.classifyName);
						courseClassify.addClass('Taxonomy').attr('classifyid',classIfyObj.classifyId).removeClass('allCourse');
					}
					else {
						
					}
				 }
			   }
		   })	   
		}
	   //面包屑导航点击事件
	   $('#breadChippingBox').delegate('a.Taxonomy','click',function(){
		       classifyId = $(this).attr('classifyId');
			   page = 1;
			   hotType = '';
			   $('#curricula').addClass('active');
			   $('.center-title li').removeClass('active');
			   getSource(pageComponents);
			   crumbsNav(classifyId)
	   })
	   //找到选中的分类高亮显示
	   function showClassIfy(result){
		   $('.bd .Taxonomy').removeClass('active');
		   $.each(result,function(index,val){
			   $.each($('.bd .Taxonomy'),function(){
				   if($(this).attr('classifyid') == val.classifyId){
					   $(this).addClass('active');
				   }
			   })
		   })
	   }
	   //课程详情页面返回的时候，我们需要解析hash来渲染面包屑导航
	   classifyId && crumbsNav(classifyId);
	  
	   //渲染侧边栏
	   function renderAside(result){
		   for(var i = 0 ; i < result.data.classifyList.length; i++){
			   if(result.data.classifyList[i].classifyName.length<=10){
				   result.data.classifyList[i].classifyNameNew = result.data.classifyList[i].classifyName;
			   }else{
				   result.data.classifyList[i].classifyNameNew = result.data.classifyList[i].classifyName.substring(0,10)+"...";
			   }
		   }
		   var asideDOM = _.template($('#courseSliderTpl').html())(result.data)
		   $('.bd').html(asideDOM); 
	   }
	  
	  
})()